import 'dart:convert';

void main() {
  // 1. 创建一个User对象
  User user = User(name: '张三', age: 18, sex: '男');
  print('姓名: ${user.name}, 年龄: ${user.age}, 性别: ${user.sex}');

  // 2. 实际开发中
  // 调用接口获取的JSON数据
  String jsonStr = '{"name": "不知火舞666", "age": 18, "sex": "女"}';
  // 将json数据转成Map对象
  Map mapData = json.decode(jsonStr);
  print('姓名: ${mapData['name']}, 年龄: ${mapData['age']}, 性别: ${mapData['sex']}');

  // 3. 创建一个User对象
  // User user2 =
  //     User(name: mapData['name'], age: mapData['age'], sex: mapData['sex']);
  // print('姓名: ${user2.name}, 年龄: ${user2.age}, 性别: ${user2.sex}');

  User user2 = User.formMap(mapData);
  print('姓名: ${user2.name}, 年龄: ${user2.age}, 性别: ${user2.sex}');
}

class User {
  String? name;
  int? age;
  String? sex;

  User({required this.name, required this.age, required this.sex});

  User.formMap(Map map) {
    this.name = map['name'];
    this.age = map['age'];
    this.sex = map['sex'];
  }
}